<?php
define("ROOT", dirname(__FILE__) );
require_once ROOT . "/includes/constants.php";
require_once ROOT . "/includes/functions.php";

define("WORKING_IN_BROWSER", !empty($_SERVER["HTTP_HOST"]));

set_include_path(
    ROOT . '/application/controllers' . PATH_SEPARATOR .
    ROOT . '/components' . PATH_SEPARATOR .
    ROOT . '/library/Doctrine' . PATH_SEPARATOR .
    ROOT . '/library/helper' . PATH_SEPARATOR .
    ROOT . '/library' . PATH_SEPARATOR .
    get_include_path() 
);

ini_set("display_errors", TRUE);
ini_set("display_startup_errors", TRUE);

require_once "Zend/Loader/Autoloader.php";
Zend_Loader_Autoloader::getInstance()->setFallbackAutoloader(TRUE);
Zend_Loader_Autoloader::getInstance()->pushAutoloader(new Zend_Loader_Autoloader_RecordTable());

$config = new Zend_Config_Ini(ROOT . "/config/common.ini", NULL, TRUE);
Zend_Registry::set("config", $config->merge(new Zend_Config_Ini(ROOT . "/config/local.ini")));

/** ЖУРНАЛ **/

$logger = new Zend_Log();
/**
 * Пишем все ошибки со статусом ERR и выше
 */
$filter = new Zend_Log_Filter_Priority( Zend_Log::NOTICE, "<=" );
$errors_writer = new Zend_Log_Writer_Stream( LOGS_PATH . "/errors.log" );
$errors_writer->addFilter( $filter );
$logger->addWriter( $errors_writer );

/**
 * Журнал только для отладочной информации
 */
$filter = new Zend_Log_Filter_Priority( Zend_Log::INFO, '>=' );
$debug_writer = new Zend_Log_Writer_Stream(LOGS_PATH . "/info.log" );
$debug_writer->addFilter( $filter );
$logger->addWriter( $debug_writer );

if( !WORKING_IN_BROWSER ) {
    $actions_writer = new Zend_Log_Writer_Stream( "php://output" );
    $logger->addWriter( $actions_writer );
}

Zend_Registry::set("log", $logger );
function _info($str) {
    Zend_Registry::get("log")->info($str);
}
function _err($str) {
    Zend_Registry::get("log")->err($str);
}

// Обработчики ошибок
set_error_handler("ErrorsController::handleError");
set_exception_handler("ErrorsController::handleException");
ini_set("display_errors", $config->server->development);
ini_set("display_startup_errors", $config->server->development);
Zend_Controller_Front::getInstance()->throwExceptions($config->server->development);

$conn = Doctrine_Manager::connection(
    sprintf(
        "%s://%s:%s@%s/%s",
        "mysql",
        "root",
        "1",
        "localhost",
        "rssizer"
   )
);
$conn->setAttribute("use_native_enum", TRUE)
->setAttribute("autoload_table_classes", TRUE);
if ($config->server->profile) {
    ini_set('max_execution_time', 0);

    $profiler = new Doctrine_Connection_Profiler();
    $conn->setListener($profiler);
}
Zend_Registry::set("connection", $conn);
unset($conn);

// Локаль
mb_internal_encoding("UTF-8");
Zend_Registry::set("locale", new Zend_Locale("ru"));

// Настройка MVC
Zend_Controller_Front::getInstance()
    ->setControllerDirectory(ROOT . "/application/controllers")

    // переопределяем плагин ErrorHandler с тем, чтобы контроллер был не «error», а «errors» — это позволит использовать его как ErrorDocument в настройках Apache
    ->registerPlugin( new Zend_Controller_Plugin_ErrorHandler( array( "controller" => "errors" ) ), 100 )
;

Zend_Layout::startMvc();
Zend_Layout::getMvcInstance()
    ->setLayoutPath(ROOT . "/application/views/layouts")
    ->setLayout("default")
    ->getView()
        ->setScriptPath(ROOT . "/application/views/scripts")
        ->addHelperPath(ROOT . "/application/views/helpers")
        ->setEncoding("UTF-8")
        ->setEscape("html_escape")
        ->doctype("XHTML1_STRICT")
;


require_once ROOT . "/application/routes.php";